/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game.experts.combine;

import analysys.HECAlignmentHit;
import game.datahandling.dataenvelopes.base.PrimaryStructure;
import game.encodings.OutputEncoding;
import game.experts.Expert;
//import utils.Msg;
import utils.Profile;
import utils.ProfileMatrix;

/**
 *
 * @author emanuele
 */
public class SSPSingleWeightHintedCombinerExpert extends SSPBaseHintedCombinerExpert {

    protected double getWeight(Expert<PrimaryStructure> e) {
        double weight, errorrate, sum;

        if (this.current.hints.isEmpty())
            return 1.0;

        sum = 0;
        for (HECAlignmentHit hint: this.current.hints) {
            String prediction = e.predict(new PrimaryStructure(hint.hitWholePri)).decode().toString();
            double errors = this.getDifferences(prediction, hint.hitWholeSec);

            double currerrorrate = errors / ((double)prediction.length());

            sum += currerrorrate;
        }

        errorrate = (sum + 0.5) / ((double)this.current.hints.size());
//        weight = 1 / (1 - accuracy);
//        weight = 1 / Math.pow((1 - accuracy), 5);
//        weight = -Math.log((1 - accuracy));
        weight = this.weightFromErrorRate(errorrate);

        return weight;
    }

    @Override
    protected Profile mergePredictions(OutputEncoding[] allPredictions) {
        Profile ret;

        ret = new ProfileMatrix(3, this.current.input.length(), 0);
        for (int i = 0; i < allPredictions.length; i++) {
            Profile p;
            double weight;

            weight = this.getWeight(this.experts.get(i));

            p = allPredictions[i].getProfile();
            p.mulBy(weight);

            ret.add(p);
        }
        ret.normalizeColumns();

        return ret;
    }
}
